//
//  MineViewController.m
//  聊吧
//
//  Created by 刘洋 on 16/6/13.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeadTableViewCell.h"
#import "MineTableViewCell.h"
#import "SettingsViewController.h"
#import "QRcodeViewController.h"

//俩个cell的静态变量
static NSString *const mineHeadCell = @"mineHeadCell";
static NSString *const mineCell = @"mineCell";

@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>
//建立tableView
@property (nonatomic, retain)UITableView *mineTableView;
@property (nonatomic, retain)QRcodeViewController *QRcodeVC;

@end

@implementation MineViewController

- (void)dealloc
{
    [_mineTableView release];
    [_QRcodeVC release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
;
   
    self.navigationItem.title = @"我";
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.hidesBackButton = YES;
    
    
    
    self.mineTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.mineTableView.backgroundColor = RGBAColor(239, 239, 244);
    [self.view addSubview:self.mineTableView];
    [_mineTableView release];
    self.mineTableView.delegate = self;
    self.mineTableView.dataSource = self;
    self.mineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //注册俩种cell
    [self.mineTableView registerClass:[MineHeadTableViewCell class] forCellReuseIdentifier:mineHeadCell];
    [self.mineTableView registerClass:[MineTableViewCell class] forCellReuseIdentifier:mineCell];
    
    
   
    
    
    
    
    
    // Do any additional setup after loading the view.
}


#pragma mark cell的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 2) {
        SettingsViewController *settingsVC = [[SettingsViewController alloc]init];
        settingsVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:settingsVC animated:YES];
        [settingsVC release];
        
    }else if (indexPath.section == 1){
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.color = [UIColor whiteColor];
        hud.labelColor = [UIColor blackColor];
        hud.labelText = @"程序猿正在日夜开发中";
        hud.dimBackground = YES;

        [hud hide:YES afterDelay:1];
        
    }
    
    
    
    
    
}


#pragma mark 设置section数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
    
}


#pragma mark tableView每个分区要显示的行数(cell的个数)必须实现的方法之一
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 2;
        
    }else{
        return 1;
        
    }
    
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.section == 2) {
           MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineCell forIndexPath:indexPath];
        cell.label.text = @"设置";
        cell.image.image = [UIImage imageNamed:@"more_icon_channelmanage"];
        cell.arrow.image = [UIImage imageNamed:@"JT"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
        
    }
    if (indexPath.section == 1) {
     
        MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineCell forIndexPath:indexPath];
        
        NSArray *array = @[@"相册",@"收藏"];
        NSArray *arrayPics = [NSArray arrayWithObjects:[UIImage imageNamed:@"more_laiwangfriend"],[UIImage imageNamed:@"more_friendscircle"],nil];
        cell.label.text = array[indexPath.row];
        //图片
        cell.image.image = arrayPics[indexPath.row];
        //箭头
        cell.accessoryType =   UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    
    
    MineHeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineHeadCell forIndexPath:indexPath];
    [cell setQRcodeBlock:^{
        self.QRcodeVC = [[QRcodeViewController alloc] init];
        [self addChildViewController:self.QRcodeVC];
        [self.view addSubview:self.QRcodeVC.view];
            [self show];
        [_QRcodeVC release];
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.headPic.image = [UIImage imageNamed:@"head"];
    cell.userName.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    
    
    return cell;
    
}

//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 80;
    }else {
        return 44;
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;


}


//二维码展开效果
-(void)show{
    [UIView animateWithDuration:0.5 animations:^{
      [self.QRcodeVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
          make.center.mas_equalTo(self.view);
          make.size.mas_equalTo(self.view).multipliedBy(0.7);
          
      }];
        
    }];
}




//section跟tableView一起滚动的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.mineTableView)
    {
        CGFloat sectionHeaderHeight = 20;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y,0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight)
        {
            scrollView.contentInset = UIEdgeInsetsMake(- sectionHeaderHeight, 0, 0, 0);
        }
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
